package com.xm.dao.impl;

import com.xm.dao.GoodsTypeDao;
import com.xm.po.GoodsType;
import com.xm.util.BaseDao;
import com.xm.util.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2020-08-07-15:21
 * @description 商品类型数据操作接口实现类
 */
public class GoodsTypeDaoImpl implements GoodsTypeDao {
    @Override
    public List<GoodsType> getAll() {
        Connection conn = DButil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<GoodsType> list = new ArrayList<>();
        String sql = "select * from goodsType";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            list = (List<GoodsType>) DButil.getRsData(GoodsType.class,rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(rs,ps,conn);
        }
        return list;
    }

    @Override
    public List<GoodsType> getGoodsTypeBySaleTop5() {
        Connection conn = DButil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<GoodsType> list = new ArrayList<>();
        String sql = "select * from goodstype where parentId = ? limit 5";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,0);
            rs = ps.executeQuery();
            list = (List<GoodsType>) DButil.getRsData(GoodsType.class,rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(rs,ps,conn);
        }
        return list;
    }
    /**
     * 根据类型名称模糊查询
     * @param typeName
     * @return
     */
    @Override
    public List<GoodsType> getGoodsTypeByName(String typeName) {
        List<GoodsType> list = (List<GoodsType>) DButil.queryByCondition(typeName);
        return list;
    }

    /**
     * 查询所有类型
     * @return
     */
    @Override
    public List<GoodsType> goodsType() {
        List<GoodsType> goodsTypes = (List<GoodsType>) DButil.queryAll(GoodsType.class);
        return goodsTypes;
    }
    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public GoodsType selectById(Integer id) {
        return (GoodsType)DButil.queryOne(GoodsType.class,id);
    }
}
